package com.lzy.cloud.goods.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.*;
import com.lzy.cloud.goods.service.*;
import com.lzy.cloud.goods.service.carRent.CarRentService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 房车租赁
 *
 * @author lc
 * @version 2018/7/12
 */
@Component
public class CarRentFacade {

    @Autowired
    private CarRentService carRentService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ParamService paramService;

    @Autowired
    private CarParamService carParamService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private CarConfigService carConfigService;

    @Autowired
    private CarImageService carImageService;

    @Autowired
    private CarDraftService carDraftService;



    /**
     * 新增房车租赁
     * @param carRentDto
     * @return
     */
    public Boolean add(CarRentDto carRentDto) {
        GoodsDto goodsDto = DozerUtils.map(carRentDto,GoodsDto.class);
        //添加到goods表
        goodsDto.setType(4);
        Long id = goodsService.add(goodsDto);

        //新增车型
        Ensure.that(id).isNotNull("00020001");
        //新增车型附属信息
        addCarMessage(id,carRentDto);
        return true;
    }

    private Boolean addCarMessage(Long id, CarRentDto carRentDto) {
        //新增房车基本参数
        //自定义数量最多100个
        ParamDto paramDto = new ParamDto();
        paramDto.setSourceId(id);
        paramDto.setSourceType(2);
        paramDto.setType(2);
        List<ParamDto> paramDtoList = paramService.selectList(paramDto);
        if(paramDtoList != null && paramDtoList.size() > 0) {
            Ensure.that(paramDtoList.size()).isNotGt(100, "00020002");
        }
        //新增到参数值表
        for (CarParamDto carParamDto:carRentDto.getCarParamDtoList()) {
            //如果这条为自定义参数
            if (carParamDto.getParamDto().getType() == 2){
                ParamDto paramDto1 = paramService.selectBySourceId(carParamDto.getParamDto());
                if (paramDto1 == null){
                    Long paramId = paramService.add(carParamDto.getParamDto());
                    carParamDto.setParamId(paramId);
                }else {
                    carParamDto.setParamId(paramDto1.getId());
                }
            }
            carParamDto.setSourceType(2);
            carParamDto.setSourceId(id);
            carParamService.add(carParamDto);
        }

        //新增车内配置(固定/自定义)
        List<CarConfigDto> carConfigDtoList = carRentDto.getCarConfigDtoList();
        if (carConfigDtoList != null && carConfigDtoList.size()>0){
            //自定义数量最多100个
            ConfigDto cDto = new ConfigDto();
            cDto.setCarId(id);
            cDto.setSourceType(2);
            cDto.setMode(2);
            PageInfo<ConfigDto> pageInfo = configService.selectList(null,cDto);
            if(pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
                Ensure.that(pageInfo.getList().size()).isNotGt(100, "00020003");
            }
            //新增到配置表
            for (CarConfigDto carConfigDto:carConfigDtoList) {
                //如果这条为自定义配置
//                if (carConfigDto.getConfigDto().getMode() == 2){
//                    carConfigDto.getConfigDto().setCarId(id);
//                    ConfigDto configDto = configService.selectByCarId(carConfigDto.getConfigDto());
//                    if (configDto == null){
//                        Long configId = configService.add(carConfigDto.getConfigDto());
//                        carConfigDto.setConfigId(configId);
//                    }else {
//                        carConfigDto.setConfigId(configDto.getId());
//                    }
//
//                }
                carConfigDto.setSourceType(2);
                carConfigDto.setSourceId(id);
                carConfigService.add(carConfigDto);
            }
        }

        //新增车型主图
        if(carRentDto.getCarImageDtoList().size()>0) {
            for (CarImageDto carImageDto:carRentDto.getCarImageDtoList()){
                carImageDto.setSourceType(2);
                carImageDto.setSourceId(id);
                carImageService.add(carImageDto);
            }
        }
        //保存为草稿
        if (carRentDto.getDraft() == 1){
            CarDraftDto carDraftDto = DozerUtils.map(carRentDto, CarDraftDto.class);
            carDraftDto.setName(carRentDto.getDraftName());
            carDraftDto.setShopId(carRentDto.getShopId());
            carDraftDto.setSourceType(2);
            carDraftDto.setSourceId(id);
            carDraftService.add(carDraftDto);
        }
        //设置外键属性新增到租赁表
        carRentDto.setId(id);
        //如果在数据库中查询到该ID的为更新
        CarRentDto crDto = carRentService.selectById(id);
        if (crDto != null ){
            carRentService.update(carRentDto);
        }else {
            carRentService.add(carRentDto);
        }

        return true;
    }

    /**
     * 根据ID得到房车租赁详情
     * @param id
     * @return
     */
    public CarRentDto getDetail(Long id) {
        CarRentDto carRentDto = carRentService.selectById(id);
        Ensure.that(carRentDto).isNotNull("该房车不存在");
        //查询房车展示图
        CarImageDto carImageDto = new CarImageDto();
        carImageDto.setSourceId(id);
        carImageDto.setSourceType(2);
        List<CarImageDto> carImageDtoList = carImageService.selectList(carImageDto);
        carRentDto.setCarImageDtoList(carImageDtoList);
        //查询房车参数关联数据
        CarParamDto carParamDto = new CarParamDto();
        carParamDto.setSourceId(id);
        carParamDto.setSourceType(2);
        List<CarParamDto> carParamDtoList = carParamService.selectList(carParamDto);
        if(carParamDtoList != null && carParamDtoList.size() > 0){
            for (CarParamDto cpDto:carParamDtoList) {
                ParamDto paramDto = paramService.selectById(cpDto.getParamId());
                cpDto.setParamDto(paramDto);
            }
        }
        carRentDto.setCarParamDtoList(carParamDtoList);
        //查询车内配置详情数据
        CarConfigDto carConfigDto = new CarConfigDto();
        carConfigDto.setSourceId(id);
        carConfigDto.setSourceType(2);
        List<CarConfigDto> carConfigDtoList = carConfigService.selectList(carConfigDto);
        if(carConfigDtoList != null && carConfigDtoList.size() > 0){
            for (CarConfigDto ccDto:carConfigDtoList) {
                ConfigDto configDto = configService.selectById(ccDto.getConfigId());
                ccDto.setConfigDto(configDto);
            }
        }
        carRentDto.setCarConfigDtoList(carConfigDtoList);
        return carRentDto;
    }

    /**
     * 编辑车型
     * @param dto
     * @return
     */
    public Object update(CarRentDto dto) {
        /*更新房车展示图*/
        //删除之前的房车展示图
        CarImageDto ciDto = new CarImageDto();
        ciDto.setSourceId(dto.getId());
        ciDto.setSourceType(2);
        carImageService.delByKeyTrue(ciDto);
        /*房车参数关联数据*/
        //删除之前的房车参数关联数据
        CarParamDto carParamDto = new CarParamDto();
        carParamDto.setSourceId(dto.getId());
        carParamDto.setSourceType(2);
        carParamService.delByKeyTrue(carParamDto);
        ParamDto paramDto = new ParamDto();
        paramDto.setSourceType(2);
        paramDto.setSourceId(dto.getId());
        paramService.delByKeyTrue(paramDto);
        /*删除车内配置详情*/
        //删除之前的车内配置详情数据
        CarConfigDto carConfigDto = new CarConfigDto();
        carConfigDto.setSourceId(dto.getId());
        carConfigDto.setSourceType(2);
        carConfigService.delByKeyTrue(carConfigDto);
        ConfigDto configDto = new ConfigDto();
        configDto.setSourceType(2);
        configDto.setCarId(dto.getId());
        configService.delByKeyTrue(configDto);
        //新增
        addCarMessage(dto.getId(),dto);
        return true;
    }
}
